#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
LastEditTime: 2024-12-16 16:08:41
Description: 
'''

__all__= []

#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
LastEditTime: 2024-12-11 10:15:18
Description: 导入主模型
'''

__all__= []
import json
import copy
from PyQt5 import QtCore, QtWidgets, QtGui

import gui.controls.envs as envs
from ..actions import Actions

global SHOW_DLG
SHOW_DLG = None

class ImportMainModel(QtWidgets.QDialog):
    
    def __init__(self):
        super().__init__()
        global SHOW_DLG
        py3d = envs.GL.get_value('py3d')
        super().__init__(py3d)
        if SHOW_DLG != None: return
        SHOW_DLG = self
        self.ui = _UI()
        self.ui.setupUi(self)
        # 重新启用信号
        self.ui.sigOnSave.connect(self.on_save)
        self.finished.connect(self.closeDialog)
        self.init()
        self.show() 
    def init(self):
        data = Actions.get_data() or {}
        # 主模型和主模型的参数计算文件
        data['mainModel'] = data.get('mainModel', "")
        data['mainModelCalFile'] = data.get('mainModelCalFile', "")
        self.ui._init(copy.deepcopy(data))
    def on_save(self, data:dict={}):
        self.close()
        Actions.save_data({
            "mainModelCalFile": data.get("mainModelCalFile") or "",
            "mainModel": data.get("mainModel") or "",
        })
        # 注意添加完了 需要更新tree 中对应位置的数据节点 用于显示
        Actions.update_tree("mainModel")

    def closeDialog(self):
        global SHOW_DLG
        SHOW_DLG = None
        self.deleteLater()

class _UI(QtWidgets.QWidget):
    sigOnSave = QtCore.pyqtSignal(dict)
    def __init__(self):
        super().__init__()
        self.parent = None
        self.__data = {}
    #自定义消息
    def setupUi(self, parent):
        # 纵向布局
        self.parent = parent
        self.__ui = envs.UIForm.UiCustomForm(self.__get_ui_config(parent))
        self.__ui._setup_ui(parent)
        # 自适应界面大小
        parent.adjustSize()
    def _init(self, data:dict={}) -> None:
        self.__data = data
        self.__ui._sets(data)

    def __get_ui_config(self, parent) -> json:
        return {
            "title": "导入模型", # window title 
            "icon": "icon.png", # window icon
            "objectName": "test", # window objectName
            "layout":{ # window 主布局
                "margin": [10, 10, 10, 10], # window 主布局的margin
                "space": 10,  # window 主布局的 Space
                "objectName": "qLayout", # window 主布局的 objectName
            },
            "items":[
                {
                    "type": "file",
                    "key":"mainModel",
                    "typeParams": { # 不同的类型所需要的参数不同，由typeParams来传递
                        "title": "模型文件",
                        "fileType": "INP Files (*.inp)"
                    },
                    "styleParams":{ # 控制样式的参数
                        "margin": [0, 0, 0, 0],
                        "space": 10,
                        "minWidth": 400,
                    }
                },
                {
                    "type": "file",
                    "key":"mainModelCalFile",
                    "typeParams": { # 不同的类型所需要的参数不同，由typeParams来传递
                        "title": "计算文件",
                        "fileType": "All Files (*)"
                    },
                    "styleParams":{ # 控制样式的参数
                        "margin": [0, 0, 0, 0],
                        "space": 10,
                        "minWidth": 400,
                    }
                },
                {},
                {
                    "type": "tools",
                    "typeParams": { # 不同的类型所需要的参数不同，由typeParams来传递
                        "items": [
                            {
                                "title": "确定",
                                "action": lambda: self.__on_save(),
                            },
                            {
                                "title": "取消",
                                "action":lambda: self.parent.close(),
                            }
                        ],
                    },
                    "styleParams":{ # 控制样式的参数
                        "margin": [0, 0, 0, 0],
                        "space": 10,
                    }
                },
            ]
        }
    def __on_save(self):
        data = self.__ui._gets()
        mainModel = data.get("mainModel")
        mainModelCalFile = data.get("mainModelCalFile")
        if not mainModel:return envs.MBox.warning("请选择模型文件")
        if not mainModelCalFile:return envs.MBox.warning("请选择主模型对应的计算文件")
        self.sigOnSave.emit(data)

        